﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;
using System.Diagnostics;

namespace Jumpman.src
{
    public class CollisionAction:IAction
    {
        public void DoAction(GameController gc)
        {

            foreach (GameElement gE in gc.CurrentStage.ListOfGameElements)
            {
                foreach (Blocks b in gc.CurrentStage.ListOfBlocks)
                {
                    //******MOVEMENT ON Y AXIS

                    
                    //if ge and block collided on sides top topleft and topright and block is lower than ge then falling is false and ge sits right above the block.
                    if (
                        Physics.SpritesCollided(gE.ElementSprite, b.DrawableSprite)
                        && (Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.Top
                        || Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.TopLeft
                        || Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.TopRight)
                        && b.DrawableSprite.Y > gE.ElementSprite.Y+gE.ElementSprite.Height -50
                        )
                    {

                        gE.IsFalling = false;
                        gE.ElementSprite.Y = (b.DrawableSprite.Y - gE.ElementSprite.Height + 13);


                    }

                   //if ge and block is not colliding and block is +/-45 from ge's x and block is lower than ge. then ge is falling.
                    else if (
                        !Physics.SpritesCollided(gE.ElementSprite, b.DrawableSprite)
                        && ((b.DrawableSprite.X + 45 >= gE.ElementSprite.X)
                        && (b.DrawableSprite.X - 45 < gE.ElementSprite.X)
                        && b.DrawableSprite.Y >= gE.ElementSprite.Y + gE.ElementSprite.Height)
                        )
                    {
                        
                        gE.IsFalling = true;
                    }
                    //if collided bottom bottomleft and bottom right and block's x is +/-45 from ge's x and block's y + height of block is equal to or slightly lower than ge's y but block's y is higher than ge's y.
                    if (
                        (Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.Bottom
                        || Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.BottomLeft
                        || Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.BottomRight)
                        && ((b.DrawableSprite.X + 45 >= gE.ElementSprite.X)
                        && (b.DrawableSprite.X - 45 < gE.ElementSprite.X)
                        && (b.DrawableSprite.Y + 50 >= gE.ElementSprite.Y + 10)
                        && (b.DrawableSprite.Y <= gE.ElementSprite.Y))
                        )
                    {
                        gE.ElementSprite.Velocity.Y = 0;
                    }

                    //*****MOVEMENT ON X AXIS

                    if (
                        !(Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.Top
                        || Physics.GetSideForCollisionTest(gE.ElementSprite.Velocity) == CollisionSide.Bottom
                        )
                        && (Physics.SpritesCollided(gE.ElementSprite,b.DrawableSprite))

                        )
                    {
                        if (b.DrawableSprite.X < gE.ElementSprite.X)
                        {
                            

                            gE.ElementSprite.Velocity.X = 1;

                            
                        }
                        else if (b.DrawableSprite.X  > gE.ElementSprite.X)
                        {
                          
                            gE.ElementSprite.Velocity.X = -1;

                            
                        }
                        
 
                    }

                }

            }
        }
    }
}